#include "Sound.h"
#include "AudioManager.h"

#include <stdlib.h>

Sound::Sound(std::string name, std::string filePath) : _name(name)
{
	soundSource = AudioManager::Instance()->getEngine()->addSoundSourceFromFile(filePath.c_str()); 
	if(!soundSource)
	{
		throw "Could not initiate file";
	}
	sound = 0;
	sound = AudioManager::Instance()->getEngine()->play2D(soundSource, false, true, true);
	if(!sound)
	{
		throw "Could not play file";
	}
	_volume = sound ->getVolume();
}

Sound::~Sound()
{
	//AudioManager::Instance()->getEngine()->removeSoundSource(sound);
	soundSource->drop();
	soundSource = 0;
}
std::string Sound::getName() const
{
	return _name;
}

void Sound::play()
{
	if(sound->getIsPaused())
	{
		sound->setIsPaused(false);
	}else{
		sound = AudioManager::Instance()->getEngine()->play2D(soundSource, false, false, true);
	}
}

void Sound::stop()
{
	sound->stop();
}

bool Sound::getIsLooped()
{
	return sound->isLooped();
}
void Sound::setIsLooped(bool loop)
{
	sound->setIsLooped(loop);
}

void Sound::setVolume(int newVolume)
{
	//check the value for between 0 and 100
	if(newVolume < 0 || newVolume > 100)
        _volume = 50;
    else
        _volume = newVolume;
    sound->setVolume(_volume/100.0);
}
void Sound::increaseVolume()
{
	_volume += 10;
    if(_volume > 100)
        _volume = 100;
    sound->setVolume(_volume/100.0);
}
void Sound::increaseVolume(int increment)
{
	_volume += increment;
    if(_volume > 100)
        _volume = 100;
    sound->setVolume(_volume/100.0);
}
void Sound::decreaseVolume()
{
	_volume -= 10;
    if(_volume < 0)
        _volume = 0;
    sound->setVolume(_volume/100.0);
}
void Sound::decreaseVolume(int increment)
{
	_volume -= increment;
    if(_volume < 0)
        _volume = 0;
    sound->setVolume(_volume/100.0);
}
int Sound::getCurrentVolume() const
{
	return _volume;
}

bool Sound::isSoundPlaying()
{
	return !(sound->isFinished());
}